import java.util.Arrays;
import java.util.HashMap;

/**
 * @author hewei
 * @version 1.0
 * @description: 1010. 总持续时间可被 60 整除的歌曲
 * @date 2022/10/13 14:52
 */

public class NumPairsDivisibleBy60 {

    public int numPairsDivisibleBy601(int[] time) {
        Arrays.sort(time);
        HashMap<Integer, Integer> map = new HashMap<>();
        int max = 0;
        for (int i : time) {
            map.put(i, map.getOrDefault(i, 0 ) + 1);
            max = Math.max(max, i);
        }
        int ans = 0;
        for (int t : time) {
            map.put(t, map.get(t) - 1);
            if (map.get(t) == 0) map.remove(t);
            int temp = 60 - (t % 60);
            while (temp + t <= 1000 && temp <= max) {
                if (map.containsKey(temp)) ans += map.get(temp);
                temp += 60;
            }
        }
        return ans;
    }

    public int numPairsDivisibleBy60(int[] time) {
        int n = time.length;
        int[] mod = new int[n];
        int ans = 0;
        for (int t : time) {
            int temp = 60 - t % 60;
            ans += mod[temp == 60 ? 0 : temp];
            mod[t % 60]++;
        }
        return ans;
    }
}
